/*
 * Powered By [pkuWx]
 * Web Site: http://www.wuxi.ss.pku.edu.cn
 * Since 2015 - 2015
 */

package cn.edu.pku.wxlib.adminuser.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.edu.pku.wxlib.admintorole.dao.AdmintoroleDao;
import cn.edu.pku.wxlib.admintorole.vo.query.AdmintoroleQuery;
import cn.edu.pku.wxlib.adminuser.dao.AdminuserDao;
import cn.edu.pku.wxlib.adminuser.model.Adminuser;
import cn.edu.pku.wxlib.admintorole.model.*;
import cn.edu.pku.wxlib.adminuser.vo.query.AdminuserQuery;
import cn.edu.pku.wxlib.role.dao.RoleDao;
import cn.edu.pku.wxlib.role.model.Role;
import cn.org.rapid_framework.page.PageRequest;
import javacommon.base.BaseManager;
import javacommon.base.EntityDao;
import javacommon.page.Page;

/**
 * @version 1.0
 * @since 1.0
 */

@Service
@Transactional
public class AdminuserManager extends BaseManager<Adminuser, AdminuserQuery, java.lang.Integer>
		implements AdminuserService {

	@Autowired
	private AdminuserDao adminuserDao;
	private AdmintoroleDao admintoroleDao;
	private RoleDao roleDao;
	public void setRoleDao(RoleDao roleDao) {
		this.roleDao = roleDao;
	}

	public void setAdminuserDao(AdminuserDao dao) {
		this.adminuserDao = dao;
	}
	
	public void setAdmintoroleDao(AdmintoroleDao admintoroleDao) {
		this.admintoroleDao = admintoroleDao;
	}

	public EntityDao getEntityDao() {
		return this.adminuserDao;
	}

	@Transactional(readOnly = true)
	public Page findPage(AdminuserQuery query) {
		return adminuserDao.findPage(query);
	}
	
	public List<Adminuser> findById(java.lang.Integer id){
		return adminuserDao.findById(id);
	}
	
	public List<Role> listRoleByAdmin(Integer adminId){
		AdmintoroleQuery atrQuery = new AdmintoroleQuery();
		Role role = new Role();
		atrQuery.setAdminId(adminId);
		List<Admintorole> atrList = admintoroleDao.findAllByQuery(atrQuery);
		List<Role> roleList = new ArrayList<Role>();
		if(atrList!=null){
			for(int i = 0; i < atrList.size(); i++){
				role = (Role) roleDao.getById(atrList.get(i).getRoleId());
				if(role.getState()!=0)//非禁用才显示
					roleList.add(role);
			}
		}
		return roleList;
	}

	//改变用户状态
	public void changeState(final Object... params) {
		adminuserDao.changeState(params);
    }
	//判断用户是否已存在
	public boolean checkExist(final Object... params) {
		if(adminuserDao.checkExist(params)!=0)
			return true;
		return false;
	}
	//查询用户未分配的角色
	public Page listUnknownRoleByAdmin(PageRequest<Map> pageRequest) {
		return roleDao.listUnknownRoleByAdmin(pageRequest);
	}

	//分配角色
	public void assignRoles(Integer adminId, Integer[] items) {
		if (adminId != null && items != null && items.length != 0) {
            Admintorole adminToRole;
            for (int i = 0; i < items.length; i++) {
            	adminToRole = new Admintorole();
            	adminToRole.setAdminId(adminId);
            	adminToRole.setRoleId(items[i]);
            	admintoroleDao.saveOrUpdate(adminToRole);
            }
        }
	}
	//根据用户Id查找role,返回page
	public Page listRoleByAdmin(AdminuserQuery query) {
		return roleDao.listRoleByAdmin(query);
	}
	//删除角色
	public void deleteRoles(Integer adminId, Integer[] items) {
		if (adminId != null && items != null && items.length != 0) {
            Admintorole adminToRole;
            for (int i = 0; i < items.length; i++) {
            	admintoroleDao.deleteRole("adminId", adminId, "roleId", items[i]);
            }
        }
	}
}
